package com.qdu.servlet;

import com.qdu.dao.UsersDao;
import com.qdu.entity.Activity;
import com.qdu.entity.Log;
import com.qdu.entity.Management;
import com.qdu.entity.Users;
import com.qdu.service.ActivityService;
import com.qdu.service.impl.LogServiceImpl;
import com.qdu.service.impl.ManagementServiceImpl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 管理员登录后的servlet
 *
 * @author Lenovo
 */
@WebServlet("/mrs")
public class managerRegisterServlet extends HttpServlet{

    ManagementServiceImpl manageImpl=new ManagementServiceImpl();
    LogServiceImpl logserviceimpl=new LogServiceImpl();
    ActivityService activityService=new ActivityService();
    UsersDao userdao=new UsersDao();
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
        
        String name=req.getParameter("name");//用户名
        String pwd=req.getParameter("pwd");//密码
        List<Management> man=manageImpl.getAllManagements();
        List<Activity> activity=activityService.findAllActivity();
        List<Users> users=userdao.getAllUsers();
        List<Log> logList=logserviceimpl.getAllLog();
        List<Users> list=new ArrayList<>();
        for(Users u:users)//只显示通过审核的
        {
            if(u.getUstatus()==1)
            {
                list.add(u);
            }
        }
        
        for(Management m:man)
        {
            if(m.getName().equals(name)&&m.getPassword().equals(pwd))
            {
                HttpSession session=req.getSession();//创建session
                session.setAttribute("name",name);
                session.setAttribute("pwd",pwd);//将用户名和密码放在session里
                System.out.println("产生session");
                req.setAttribute("managerlist", activity);
                req.setAttribute("logList", logList);//将所有日志放入属性
                req.setAttribute("userList",list);
                req.getRequestDispatcher("manager.jsp").forward(req, resp);
                return;
            }
        }
//        
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().println("<h3 style=\"color:red;position:absolutely;top:500px;left:700px;\">用户名或密码错误<h3>");
    }
}
